﻿#pragma once

#include<iostream>

using namespace std;


enum Colour
{
	RED,
	BLACK
};

template<class T>
struct RBTreeNode
{//结构体默认private私有
	T _data;//数据
	RBTreeNode<T>* _left;//左
	RBTreeNode<T>* _right;//右
	RBTreeNode<T>* _parent;//亲
	Colour _col;

	RBTreeNode(const T& data)
		:_data(data)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{}
};

template<class T, class Ref, class Ptr>//Ref相当于T&,Ptr相当于T*
//为了避免代码重复，后续实现consr_iterator只需将Ref替换为const T&w
struct RBTreeIterator
{
	typedef RBTreeNode<T> Node;
	typedef RBTreeIterator<T, Ref, Ptr> Self;

	Node* _node;
	Node* _root;

	RBTreeIterator(Node* node, Node* root)
		:_node(node)
		,_root(root)
	{}

	Self& operator++()//RBTreeIterator类型,前缀++
	{//红黑树节点往右找
		//1有右节点找右节点最小节点(最左子节点)
		//2右子节点不存在，找亲节点，直到找到为亲左子节点，那就为亲

		if (_node == nullptr)
			return *this;

		if (_node->_right)//1右子节点存在,找右子节点最小节点
		{
			Node* min = _node->_right;
			while (min->_left)//左子节点为空停止
			{
				min = min->_left;
			}

			_node = min;
		}
		else//2右子节点不存在，找亲节点，直到找到为亲左子节点，那就为亲
		{
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && parent->_right == cur)//若为亲右节点且亲节点存在就一直循环
			{
				cur = parent;
				parent = cur->_parent;
			}

			_node = parent;
		}

		return *this;//返回当前迭代器的引用
	}

	Self& operator--()//前缀--
	{//根据已有++，推断，当最右节点时++，向上寻找直到根，根亲为空，停止寻找，_node为空
		if (_node == nullptr)
		{//为空，表示到了最右节点，--找最右节点
			Node* cur = _root;
			while (cur && cur->_right)
			{
				cur = cur->_right;
			}
			_node = cur;

			return *this;
		}

		if (_node->_left)//跟++相反
		{
			Node* max = _node->_left;
			while (max->_right)
			{
				max = max->_right;
			}

			_node = max;
		}
		else
		{
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && parent->_left == cur)
			{
				cur = parent;
				parent = cur->_parent;
			}

			_node = parent;
		}

		return *this;
	}

	// 后缀++
	Self operator++(int)
	{
		Self temp = *this;
		++(*this);
		return temp;
	}

	// 后缀--
	Self operator--(int)
	{
		Self temp = *this;
		--(*this);
		return temp;
	}

	Ref operator*()
	{
		return _node->_data;
	}

	Ptr operator->()
	{
		return &_node->_data;
	}

	bool operator!= (const Self& s) const
	{
		return _node != s._node;
	}

	bool operator== (const Self& s) const
	{
		return _node == s._node;
	}
};




template<class K, class T, class KeyOfT>
class RBTree
{
	typedef RBTreeNode<T> Node;
public:
	typedef RBTreeIterator<T, T&, T*> Iterator;
	typedef RBTreeIterator<T, const T&, const T*> ConstIterator;

	Iterator Begin()
	{//最左
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return Iterator(cur, _root);
	}

	Iterator End()
	{//我们设定为空
		return Iterator(nullptr, _root);
	}

	ConstIterator Begin() const
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return ConstIterator(cur, _root);
	}

	ConstIterator End() const
	{
		return ConstIterator(nullptr, _root);
	}

	pair<Iterator,bool> Insert(const T& data)
	{
		if (_root == nullptr)//若为空树初始化
		{
			_root = new Node(data);//初始化插入节点为根节点
			_root->_col = BLACK;

			return { Iterator(_root,_root),true };//往上翻找初始化有两个节点一个_node一个_root
		}										//若树为空，插入节点为根节点(_root),也为指向节点(_node)
												//故返回两个_root值即可


		//若不为空树
		KeyOfT kot;//在外部定义函数，用来返回指向节点数据，set为key,map为kv.first
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kot(cur->_data) > kot(data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (kot(cur->_data) < kot(data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return { Iterator(cur,_root),false };
			}
		}

		cur = new Node(data);
		Node* newnode = cur;
		cur->_col = RED;
		if (kot(parent->_data) > kot(data))
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;

		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			if (grandfather->_left == parent)
			{
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{
					grandfather->_col = RED;
					parent->_col = uncle->_col = BLACK;

					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_left)
					{
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}

					break;
				}
			}
			else
			{
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == RED)
				{
					grandfather->_col = RED;
					parent->_col = uncle->_col = BLACK;
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
		}

		_root->_col = BLACK;

		return { Iterator(newnode,_root),true };
	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
		{
			subRL->_parent = parent;
		}
		Node* parentParent = parent->_parent;
		subR->_left = parent;
		parent->_parent = subR;

		if (parentParent == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}

			subR->_parent = parentParent;
		}
	}

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
		{
			subLR->_parent = parent;
		}
		Node* parentParent = parent->_parent;
		subL->_right = parent;
		parent->_parent = subL;

		if (parentParent == nullptr)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subL;
			}
			else
			{
				parentParent->_right = subL;
			}

			subL->_parent = parentParent;
		}
	}

private:
	Node* _root = nullptr;
};














